<?php require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex"); 
require_once($_SERVER['DOCUMENT_ROOT']."/Modules/MX23DF2S/modconf.php"); 
MYTCONN(); 
	//==========================Vars===========================		
	$TimeStart = $_POST['TStart'];
	$TimeEnd = $_POST['TEnd'];
	$Currency = null;
	$Type = null;
	$Origen0 = null;
	$QOrigen0 = null;
	$Origen1 = null;
	$QOrigen1 = null;
	$Status = null;
	$QStatus = null;
	$IsAlbaran = '';

	$OrderAD1 = 'ASC';
	$OrderAD2 = 'ASC'; 
	$OrderAD3 = 'ASC';

	$OrderBy1 = '`ref` '.$OrderAD1; 
	$OrderBy2 = null; 
	$OrderBy3 = null;

	if(isset($_POST['Currency']) && $_POST['Currency'] != null) {
	$Currency = ' AND `ref_currency_in` = '.$_POST['Currency'];
	}
	if(isset($_POST['Type']) && $_POST['Type'] != null) {
	$Type = ' AND `Type` = '.$_POST['TYPE'];
	}
	if(isset($_POST['ExcOrigen0']) && $_POST['ExcOrigen0'] != null) {
	$Origen0 = $_POST['ExcOrigen0'];
	$QOrigen0 = ' AND `ExcOrigen0` IN (SELECT `OriNumero` FROM `origenes` WHERE `OriDominio` = '.$Origen0.')';
	}
	if(isset($_POST['ExcOrigen1']) && $_POST['ExcOrigen1'] != null) {
	$Origen1 = $_POST['ExcOrigen1'];
	$QOrigen1 = ' AND `ExcOrigen1` = '.$Origen1;
	}
	if(isset($_POST['ExcStatus']) && $_POST['ExcStatus'] != null) {
	$Status = $_POST['ExcStatus'];
	$QStatus = ' AND `ExcStatus` = '.$Status;
	}
	
	if($_POST['OrderBy1'] != null) {
	$OrderBy1 = '`'.$_POST['OrderBy1'].'` '.$_POST['OrderAD1'];
	}
	if($_POST['OrderBy2'] != null) {
	$OrderBy2 = ',`'.$_POST['OrderBy2'].'` '.$_POST['OrderAD2'];
	}
	if($_POST['OrderBy3'] != null) {
	$OrderBy3 = ',`'.$_POST['OrderBy3'].'` '.$_POST['OrderAD3'];
	}
	if(isset($_POST['VIEWColumns'])) { 
		$Colums = $_POST['VIEWColumns'];
		$CCols = count($_POST['VIEWColumns']);
	}
	if ($_POST['IsAlbaran'] == 0) {
	$IsAlbaran = " AND malote = 0";
	} elseif ($_POST['IsAlbaran'] == 1) {
	$IsAlbaran = " AND malote > 0";
	}
if(isset($Colums)) {

  $QColums = '';
  for ($x = 0; $x < $CCols; $x++) {
  	if ($Colums[$x] == 'status') {
		$QColums .= ',`sg_Abreviatura` AS status';
	} else {
		$QColums .= ',`'.$Colums[$x].'`';
	}
  } 
 if (isset($_POST['BBar_Status'])) {
 $QColums .= ',`FraCargo`';
 }
 
 
 /*"SELECT 
					SUM(exchange_profit.Rate*exchange_operations.Amount) AS tax_bpn,
					MAX(exchange_operations.profit_ref) AS profit_ref,
					{$consultType}.ref AS ID,type_ref,
					SUM(exchange_operations.Amount) AS Amount,
					SUM(exchange_operations.acerto) AS acerto,
					SUM(IF(type_ref=1,1/exchange_rates.RATEOUT*exchange_operations.Amount,1/exchange_rates.RATEIN*exchange_operations.payed)) AS StartAmount,
					MAX({$consultType}.{$TimeType}) AS Time,
					MAX(exchange_status.NAME) AS Status,
					MAX(main_offices.name) AS Office,
					MAX(main_users.name) AS User,
					exchange_operations.in_currency_ref,
					exchange_operations.out_currency_ref,
					In_Curr.miso_ID AS in_curr,
					Out_Curr.miso_ID AS out_curr,
					SUM(exchange_operations.tax1*exchange_operations.Amount) AS tax1
						
					AND ({$consultType}.ref > 0)
					{$QrStat}
					{$Off}
					{$Crr}
					{$RatS}{$RatE}
					GROUP BY {$consultType}.ref";*/
					
$POOLOrders = $GLOBALS['MYT']->db_array("SELECT `exchange_operations`.`ref` AS ExcID,
				`exchange_operations`.`Time` AS ExcFechaIngreso,
				`exchange_operations`.`Amount`,`payed`,`acerto`,`in_currency_ref`,`out_currency_ref`
  					{$QColums}
  			FROM exchange_operations
				INNER JOIN exchange_status ON exchange_status.ID = exchange_operations.Status
				INNER JOIN exchange_rates ON exchange_rates.ref = exchange_operations.rates_ref
				LEFT OUTER JOIN exchange_bags ON exchange_bags.ref = exchange_operations.malote
				LEFT OUTER JOIN exchange_profit ON exchange_profit.ref = exchange_operations.profit_ref
				LEFT OUTER JOIN exchange_depositos ON exchange_depositos.REF = exchange_operations.dep_ref
				LEFT OUTER JOIN systemoffice.main_offices ON main_offices.ref = exchange_operations.main_office_ref
				LEFT OUTER JOIN systemoffice.main_users ON main_users.ref = exchange_operations.RefUser
			WHERE DATE(`exchange_operations`.`Time`) BETWEEN '{$TimeStart}' AND '{$TimeEnd}'
			{$IsAlbaran}{$Type}{$QOrigen0}{$QOrigen1}{$QStatus}
			ORDER BY {$OrderBy1}{$OrderBy2}{$OrderBy3}");
	$CARDOrders = $POOLOrders[0]; ?>
<div id="ConsoleRender"></div>
<script charset="utf-8" type="text/javascript">
disableSelection(document.getElementById("Console")) //Disable text selection on element with id="mydiv"

Ext.onReady(function(){

 $elem = 280;
 var  windowWidth = 0, windowHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	windowWidth = window.innerWidth
	windowHeight = window.innerHeight-$elem;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	windowWidth = document.documentElement.clientWidth;
	windowHeight = document.documentElement.clientHeight-$elem;
  } 

    Ext.QuickTips.init();

    var myData = [
	<?php   
        $sums['EUR'] = 0;
		$sums['USD'] = 0;
		$sums['BRL'] = 0;
	for ($i=1;$i<=$CARDOrders;$i++) { 
        $sums['EUR'] = $sums['EUR']+$POOLOrders[$i]['Amount'];
		$sums['USD'] = $sums['USD']+$POOLOrders[$i]['payed'];
		$sums['BRL'] = $sums['BRL']+$POOLOrders[$i]['acerto'];
		if (isset($_POST['BBar_Status'])) {
		$sums['EUR'] = $sums['EUR']+$POOLOrders[$i]['FraCargo'];
		}
   echo '['.$i.','.$POOLOrders[$i]['ExcID'].',\''.$POOLOrders[$i]['ExcFechaIngreso'].'\','.$POOLOrders[$i]['Amount'].','.$POOLOrders[$i]['payed'].','.$POOLOrders[$i]['acerto'];
		for ($x = 0; $x < $CCols; $x++) {
			$ColumnX = $Colums[$x];
				if (isset($POOLOrders[$i]['tax1']) && $POOLOrders[$i][$ColumnX] == $POOLOrders[$i]['tax1']) {
					$POOLOrders[$i][$ColumnX] = number_format($POOLOrders[$i][$ColumnX],4);
				}
				echo ',\''.$POOLOrders[$i][$ColumnX].'\'';
        }  
	echo ']';
		if ($i<$CARDOrders) {
			echo ',';
		}
    } ?>
    ];

    var ds = new Ext.data.Store({
        reader: new Ext.data.ArrayReader({}, [
               {name: 'id'},
			   {name: 'FraID', type: 'int'},
               {name: 'Data', type: 'date', dateFormat: 'Y-m-d H:i:s'},
               {name: 'mon_1', type: 'float'},
               {name: 'mon_2', type: 'float'},
               {name: 'mon_3', type: 'float'},
               {name: 'in_cur', type: 'string'},
               {name: 'out_cur', type: 'string'}
	
	<?php 
    for ($x = 0; $x < $CCols; $x++) {
			if ($Colums[$x] == 'ORAccount') {
			echo ',{name: \'finbank\', type:\'string\'}
			,{name: \'FinAgen\', type:\'string\'}
			,{name: \'FinAcTp\', type:\'string\'}
			,{name: \'FinAcNb\', type:\'string\'}';
			} else {
			echo ',{name: \''.$Colums[$x].'\', type:\'string\'}';
			}
        } ?>
          ]),
		  		loadMask: true
    });
    ds.loadData(myData);
    var colModel = new Ext.grid.ColumnModel([
        {header: "Numero", width: 80, sortable: true, locked:true, dataIndex: 'FraID'},
        {header: "Data", width: 150, sortable: true, renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'), dataIndex: 'Data'},
        {header: "Recebido", width: 75, sortable: true, locked:true, align: 'right', dataIndex: 'mon_1'},
        {header: "A pagar", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'mon_2'},
        {header: "Entregue", width: 75, sortable: true, locked:true, align: 'right',dataIndex: 'mon_3'},
        {header: "M. Recebida", width: 40, sortable: true, locked:true, align: 'right',dataIndex: 'in_cur'},
        {header: "M. Entregue", width: 40, sortable: true, locked:true, align: 'right',dataIndex: 'out_cur'}
	<?php
    for ($x = 0; $x < $CCols; $x++) {
		echo ',{header: "'.$Colums[$x].'", width: 80, sortable: true, dataIndex: \''.$Colums[$x].'\'}';
    }  ?>
    ]);
	Bars = new Ext.Toolbar({ 
				
					store: this.store, 
					height: 20,
					items : [ {
						text: "<?php echo $CARDOrders;?> Registos | EUR: <?php echo number_format($sums['EUR'],2);?> | USD: <?php echo number_format($sums['USD'],2);?> | BRL: <?php echo number_format($sums['BRL'],2);?>"
				},{
				type:'button',
				loadMask: true,
                text:'Confirmar Selecionados',
                handler:function(){
					var ss = ''; 
					var m = selmode.getSelections();				
					for(var i = 0, len = m.length; i < len; i++){
								ss += m[i].get("FraIDFraID") + ",";}
							document.FMORDNGEN.POOL.value = ss;
							document.FMORDNGEN.action='<?php echo $MODURL;?>Lib/a05/vie_console.mex';
							subform('FMORDNGEN','SUMS');
							updateCorr();
							}
					} ]
				});
   var selmode = new Ext.grid.RowSelectionModel({});
   var Console = new Ext.grid.GridPanel({
        id: 'Console',
		layout: 'fit',
	    height: windowHeight-20,
		ds: ds,
		cm: colModel,
		sm: selmode,
		listeners: {
			rowdblclick: function(ds, row) {
				var record = ds.getStore().getAt(row);
				var value = record.get('FraID');
				menuwindow('Operativo','_response/call.php?get=Operative/pp_frm&,800,520,'MnMain');
				replaceUrl('ORPanel','Modules/CLDJ49SJ/lib/frm_EX.mex?EX='+value);
			}
		},
		bbar: Bars,
        renderTo: 'ConsoleRender'
    });
});
</script>
<?php } ?>